<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    
</head>
<body>
    <button>发送</button>
    <button>取消</button>
    <script>
        /**
         *  config
         *  1. method
         *  2. url
         *  3. data
         *  4. params
         *  5. headers
         *  6. timeout
         *  7. cancelToken: new axios.CancelToken(c=>{全局变量 = c})
         * 
         */
        let sendBtn = document.querySelectorAll('button')[0];
        let cancelBtn = document.querySelectorAll('button')[1];
        let cancel = null;
        sendBtn.onclick = function(){
            cancel && cancel();
            axios.get('http://localhost:8080/timeout', {
                cancelToken:new axios.CancelToken((c)=>{
                    cancel = c;// c 是一个函数，函数调用请求就会被取消
                })
            }).then(data=>{
                console.log(data.data);
            })
        }
        cancelBtn.onclick = function(){
            cancel();
        }
    </script>
</body>
</html>